package com.redsoil.house.ui.mine.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.redsoil.house.R;
import com.redsoil.house.base.RootActivity;
import com.redsoil.house.model.bean.ProductDetailVO;
import com.redsoil.house.model.http.api.ApiConstants;
import com.redsoil.house.ui.matter.activity.MatterInfoActivity;
import com.redsoil.house.ui.matter.activity.MatterPhotoActivity;
import com.redsoil.house.ui.mine.contract.FoodsBuyContract;
import com.redsoil.house.ui.mine.contract.FoodsInfoContract;
import com.redsoil.house.ui.mine.presenter.FoodsBuyPresenter;
import com.redsoil.house.ui.mine.presenter.FoodsInfoPresenter;
import com.redsoil.house.widget.actionbar.XBaseTopBar;
import com.redsoil.house.widget.actionbar.XBaseTopBarImp;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bingoogolapple.bgabanner.BGABanner;

public class FoodsInfoActivity extends RootActivity<FoodsInfoPresenter> implements FoodsInfoContract.View,XBaseTopBarImp{

    @BindView(R.id.main_topbar)
    XBaseTopBar main_topbar;

    @BindView(R.id.goods_banner)
    BGABanner goods_banner;

    @BindView(R.id.goods_title_tv)
    TextView goods_title_tv;

    @BindView(R.id.goods_price_tv)
    TextView goods_price_tv;

    @BindView(R.id.goods_old_price_tv)
    TextView goods_old_price_tv;

    @BindView(R.id.goods_contain_tv)
    TextView goods_contain_tv;

    @BindView(R.id.buy_button)
    Button buy_button;

    private String id;

    private ProductDetailVO productDetailVO;

    @Override
    protected int getLayout() {
        return R.layout.activity_foods_info;
    }

    @Override
    protected void initEventAndData() {
        super.initEventAndData();
        id = getIntent().getStringExtra("id");
        main_topbar.setxBaseTopBarImp(this);
        stateLoading();
        mPresenter.postProductDetail(id);
    }

    @OnClick(R.id.buy_button)
    void onBuyButton(){
        startActivity(FoodsBuyActivity.newInstance(this,productDetailVO));
    }

    @Override
    public void postProductDetailSuccess(ProductDetailVO productDetailVO) {

        this.productDetailVO = productDetailVO;

        goods_banner.setAdapter(new BGABanner.Adapter<ImageView, String>() {
            @Override
            public void fillBannerItem(BGABanner banner, ImageView itemView, String model, int position) {
                Glide.with(FoodsInfoActivity.this)
                        .load(model)
                        .placeholder(R.mipmap.ic_launcher)
                        .error(R.mipmap.ic_launcher)
                        .centerCrop()
                        .dontAnimate()
                        .into(itemView);
            }
        });

        goods_banner.setDelegate(new BGABanner.Delegate<ImageView, String>() {
            @Override
            public void onBannerItemClick(BGABanner banner, ImageView itemView, String model, int position) {
                startActivity(MatterPhotoActivity.newInstance(getBaseContext(),model));
            }
        });


        List<String> list = new ArrayList<String>();
        for (int i=0;i<productDetailVO.getDpic().size();i++){
            ProductDetailVO.DpicBean bean = productDetailVO.getDpic().get(i);
            list.add(ApiConstants.STATIC_FILE+bean.getPic());
        }

        goods_banner.setData(list,
                Arrays.asList("", "", ""));

        goods_title_tv.setText(productDetailVO.getName());
        goods_price_tv.setText(""+productDetailVO.getCurrentPrice());
        goods_old_price_tv.setText("原价:"+productDetailVO.getOriginalPrice());
        goods_contain_tv.setText(productDetailVO.getIntro());

        stateMain();
    }

    @Override
    public void responeError(String errorMsg) {
        stateError();
    }

    @Override
    public void left() {
        finish();
    }

    @Override
    public void right() {}

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    /**
     * 打开新Activity
     *
     * @param context
     * @return
     */
    public static Intent newInstance(Context context,String id) {
        Intent intent = new Intent(context, FoodsInfoActivity.class);
        intent.putExtra("id",id);
        return intent;
    }

}
